//
//  AnimationView.m
//  news_ios_app
//
//  Created by apple on 16/3/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AnimationView.h"
#define CONTENTOFFSET  @"contentOffset"
#define DRAGGING        @"isDragging"


@interface AnimationView()
@property (strong,nonatomic) CAShapeLayer *tLayer;
@property (strong,nonatomic) UIBezierPath *tPath;
@property (strong,nonatomic) UIView *loadingView;
@property (weak,nonatomic) UIScrollView *scrollView;
@end



@implementation AnimationView

-(void)initializeWithFrame:(CGRect)frame contentView:(UIView *)contentView scrollView:(UIScrollView *)scrollView
{
    _tLayer = [CAShapeLayer layer];
    _tLayer.fillColor = RGBCOLOR(73, 160, 190).CGColor;
    _tLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    [self.layer addSublayer:_tLayer];
    
    if (contentView) {
        _contentView = contentView;
        [self addSubview:contentView];
    }
    
    _loadingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, BOUNDWIDTH, 200 * kPROPORTION)];
    
    _loadingView.backgroundColor = [UIColor clearColor];
    _loadingView.alpha = 0;
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100 * kPROPORTION, BOUNDWIDTH, 40)];
    
    label.text = @"进入新闻页";
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor whiteColor];
    
    [_loadingView addSubview:label];
    
    [self addSubview:_loadingView];
    
    
    _scrollView = scrollView;
    
    
    [_scrollView addObserver:self forKeyPath:CONTENTOFFSET options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    UIPanGestureRecognizer *pan = [_scrollView valueForKey:@"pan"];
    
//    UITouch *touch = pan
    
    [_scrollView addObserver:self forKeyPath:DRAGGING options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
 
}

-(void)dealloc
{
    [self removeObserver:self forKeyPath:CONTENTOFFSET];
    [self removeObserver:self forKeyPath:DRAGGING];
}


-(instancetype)initWithFrame:(CGRect)frame contentView:(UIView *)contentView scrollView:(UIScrollView *)scrollView
{
    if (self = [super initWithFrame:frame]) {
        [self initializeWithFrame:frame contentView:contentView scrollView:scrollView];
    }
    return self;
}

-(void)updatePathWithContentOffset:(CGPoint)contentOffset
{
    _loadingView.alpha = (-contentOffset.y - 200 * kPROPORTION)/80;
    _contentView.alpha = 1 - ((-contentOffset.y - 200 * kPROPORTION)/80);
    _tPath = [UIBezierPath bezierPath];
    [_tPath moveToPoint:CGPointMake(0, 0)];                          // r1点
    [_tPath addLineToPoint:CGPointMake(BOUNDWIDTH, 0)];            // r2点
    [_tPath addLineToPoint:CGPointMake(BOUNDWIDTH,  200 * kPROPORTION)];  // r4点
    [_tPath addQuadCurveToPoint:CGPointMake(0, 200 * kPROPORTION)
                    controlPoint:CGPointMake(BOUNDWIDTH/2, -contentOffset.y)]; // r3,r4,r5确定的一个弧线
    [_tPath closePath];
    _tLayer.path = _tPath.CGPath;
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //change  字典中的old  new  是关键字，专门用来存储新值和老值
    
    if ([keyPath isEqualToString:CONTENTOFFSET]) {
        
    }else if ([keyPath isEqualToString:DRAGGING])
    {
        NSLog(@"oldname %@",[change objectForKey:@"old"]);
        NSLog(@"new %@",[change objectForKey:@"new"]);
    }
    
  
}

-(void)scrollViewDidScroll:(CGPoint)contentOffset
{
    
}



@end
